package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

type Queue struct {
	front, rear int
	data        []int
}

func NewQueue(size int) *Queue {
	return &Queue{
		front: 0,
		rear:  0,
		data:  make([]int, size),
	}
}
func (a *Queue) Push(item int) {
	if a.IsFull() {
		fmt.Println("××队列满了××")
		return
	}
	a.data[a.rear] = item
	a.rear = (a.rear + 1) % len(a.data)
}
func (a *Queue) Pull() (int, bool) {
	if a.IsEmpty() {
		fmt.Println("队列是空的")
		return 0, false
	}
	i := a.data[a.front]
	a.front = (a.front + 1) % len(a.data)
	return i, true
}

func (a *Queue) IsFull() bool {
	return (a.rear+1)%len(a.data) == a.front
}
func (a *Queue) IsEmpty() bool {
	return a.rear == a.front
}
func (a *Queue) Peek() (int, bool) {
	if a.IsEmpty() {
		fmt.Println("队列是空的")
		return 0, false
	}
	return a.data[a.front], true
}
func (a *Queue) Display() {
	fmt.Printf("front: %d ,rear:%d\n", a.front, a.rear)
	for i := 0; i < len(a.data); i++ {
		fmt.Printf("[%d]=>[%d]\n", i, a.data[i])
	}
}

func main() {
	q := NewQueue(4)
	scanner := bufio.NewScanner(os.Stdin)
	displayHelpInfo()
	for scanner.Scan() {
		text := scanner.Text()
		switch text {
		case "a":
			fmt.Printf("增加一个元素，请输入:")
			for scanner.Scan() {
				i, err := strconv.Atoi(scanner.Text())
				if err != nil {
					fmt.Println(err)
					continue
				}
				q.Push(i)
				break
			}
		case "g":
			fmt.Printf("=>获取到一个元素：")
			fmt.Println(q.Pull())
		case "e":
			fmt.Printf("=>是否空：")
			fmt.Println(q.IsEmpty())
		case "f":
			fmt.Printf("=>是否满：")
			fmt.Println(q.IsFull())
		case "p":
			fmt.Printf("=>查看第一个元素：")
			fmt.Println(q.Peek())
		case "d":
			q.Display()
		}
		displayHelpInfo()
	}
}

func displayHelpInfo() {
	fmt.Println("--------help info---------")
	fmt.Println("a(push)增加一个")
	fmt.Println("g(pull)获取一个")
	fmt.Println("p(peek)查看第一个")
	fmt.Println("f(full)查看是否满")
	fmt.Println("e(empty)查看是否空")
	fmt.Println("d(display)查看所有")
}
